﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace AiBLSmartEdu.Module.SafeResponse.Middleware;

public class SafeResponseMiddleware
{
    private readonly RequestDelegate _next;
    private readonly ILogger<SafeResponseMiddleware> _logger;
    private readonly RequestDataDecryptor _requestDataDecryptor;

    public SafeResponseMiddleware(
        RequestDelegate next,
        ILogger<SafeResponseMiddleware> logger,
        RequestDataDecryptor requestDataDecryptor)
    {
        _next = next;
        _logger = logger;
        _requestDataDecryptor = requestDataDecryptor;
    }

    public async Task Invoke(HttpContext context)
    {
        _logger.LogInformation($"{nameof(SafeResponseMiddleware)}：解密");

        _requestDataDecryptor.DecryptUrlData(context);
        await _requestDataDecryptor.DecryptBodyData(context);

        await _next(context);
        _logger.LogInformation($"{nameof(SafeResponseMiddleware)}：加密");
    }
}
